package com.example.OnlineStorage.Controller.Download;

import com.example.OnlineStorage.enums.GlobalVariables_String;
import com.example.OnlineStorage.pojo.Response.ApiResponse;
import com.example.OnlineStorage.pojo.Response.ResponseData;
import com.example.OnlineStorage.service.Download.DownloadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.core.io.support.ResourceRegion;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/download")
@Tag(name = "下载相关接口")
public class DownloadController {
    private final DownloadService downloadService;

    private final String TOKEN = GlobalVariables_String.TOKEN.getValue();

    public DownloadController(DownloadService downloadService) {
        this.downloadService = downloadService;
    }

    @GetMapping("/File")
    @Operation(summary = "个人文件下载")
    @Parameters({
            @Parameter(name = "SHA_256", description = "SHA_256", required = true),
            @Parameter(name = "name", description = "要下载为的文件名称", required = true),
            @Parameter(name = "Range", description = "请求范围", in = ParameterIn.HEADER, required = false),
    })
    public ResponseEntity<ResourceRegion> Download(
            @RequestParam String SHA_256,
            @RequestParam String name,
            HttpServletRequest request) {

        String Range = request.getHeader("Range");
        String UUID = request.getHeader(TOKEN);
        ResponseData download = downloadService.Download(SHA_256, name, Range, UUID);

        return ApiResponse.GetApiResponse().binBody(download).downloadBuild();
    }

    @GetMapping("/shareFile")
    @Operation(summary = "共享文件下载")
    @Parameters({
            @Parameter(name = "shareFilesId", description = "共享文件id", required = true),
            @Parameter(name = "SHA_256", description = "SHA_256", required = true),
            @Parameter(name = "name", description = "要下载为的文件名称", required = true),
            @Parameter(name = "Range", description = "请求范围", in = ParameterIn.HEADER, required = false),
    })
    public ResponseEntity<ResourceRegion> Download(
            @RequestParam Long shareFilesId,
            @RequestParam String SHA_256,
            @RequestParam String name,
            HttpServletRequest request) {

        String Range = request.getHeader("Range");
        String UUID = request.getHeader(TOKEN);
        ResponseData download = downloadService.Download(shareFilesId, SHA_256, name, Range, UUID);

        return ApiResponse.GetApiResponse().binBody(download).downloadBuild();
    }

    @GetMapping("/FileByPath")
    @Operation(summary = "个人文件下载（根据路径）")
    @Parameters({
            @Parameter(name = "path", description = "要下载的路径只能是文件", required = true),
            @Parameter(name = "name", description = "要下载为的文件名称", required = true),
            @Parameter(name = "Range", description = "请求范围", in = ParameterIn.HEADER, required = false),
    })
    public ResponseEntity<ResourceRegion> DownloadByPath( @RequestParam  String path,
                                                          @RequestParam  String name,
                                                          HttpServletRequest request){
        String Range = request.getHeader("Range");
        String UUID = request.getHeader(TOKEN);
        return ApiResponse.GetApiResponse()
                .binBody(downloadService.DownloadByPath(path, name,Range,UUID))
                .downloadBuild();
    }

    @GetMapping("/shareFileByPath")
    @Operation(summary = "共享文件下载（根据路径）")
    @Parameters({
            @Parameter(name = "shareFilesId", description = "共享文件id", required = true),
            @Parameter(name = "path", description = "要下载的路径只能是文件", required = true),
            @Parameter(name = "name", description = "要下载为的文件名称", required = true),
            @Parameter(name = "Range", description = "请求范围", in = ParameterIn.HEADER, required = false),
    })
    public ResponseEntity<ResourceRegion> DownloadByPath( @RequestParam  Long shareFilesId,
                                                          @RequestParam  String path,
                                                          @RequestParam  String name,
                                                          HttpServletRequest request){
        String Range = request.getHeader("Range");
        String UUID = request.getHeader(TOKEN);

        return ApiResponse.GetApiResponse()
                .binBody(downloadService.DownloadByPath(shareFilesId,path, name,Range,UUID))
                .downloadBuild();
    }


    @GetMapping("/FileOnlinePreviewByPath")
    @Operation(summary = "个人文件预览（根据路径）")
    @Parameters({
            @Parameter(name = "path", description = "要预览的路径只能是文件", required = true),
            @Parameter(name = "Range", description = "请求范围", in = ParameterIn.HEADER, required = false),
    })
    public ResponseEntity<ResourceRegion> OnlinePreviewByPath (@RequestParam String path,
                                                               HttpServletRequest request){
        String Range = request.getHeader("Range");
        String UUID = request.getHeader(TOKEN);

        return ApiResponse.GetApiResponse()
                .binBody(downloadService.OnlinePreviewByPath(path,Range,UUID))
                .onlineBuild();
    }
    @GetMapping("/shareFileOnlinePreviewByPath")
    @Operation(summary = "共享文件预览（根据路径）")
    @Parameters({
            @Parameter(name = "shareFilesId", description = "共享文件id", required = true),
            @Parameter(name = "path", description = "要预览的路径只能是文件", required = true),
            @Parameter(name = "Range", description = "请求范围", in = ParameterIn.HEADER, required = false),
    })
    public ResponseEntity<ResourceRegion> OnlinePreviewByPath (@RequestParam Long shareFilesId,
                                                               @RequestParam String path,
                                                               HttpServletRequest request){
        String Range = request.getHeader("Range");
        String UUID = request.getHeader(TOKEN);
        return ApiResponse.GetApiResponse()
                .binBody(downloadService.OnlinePreviewByPath(shareFilesId,path,Range,UUID))
                .onlineBuild();
    }

}
